package com.aioz.security.service.impl;

import com.aioz.security.common.ResponseResult;
import com.aioz.security.entity.Company;
import com.aioz.security.entity.dto.PositionDto;
import com.aioz.security.mapper.CompanyMapper;
import com.aioz.security.mapper.PositionMapper;
import com.aioz.security.service.CompanyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.text.Position;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Aioz
 * @since 2022-09-27
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private PositionMapper positionMapper;

    @Autowired
    private CompanyInfoServiceImpl companyInfoService;

    @Override
    public ResponseResult getAllCompanies(Integer currentPage, Integer size) {
        Page<Company> page = new Page<>(currentPage,size);
        Page<Company> companyPage = companyMapper.selectPage(page, null);
        Map<String,Object> map = new HashMap<>();
        List<Company> companies = companyPage.getRecords();
        long total = companyPage.getTotal();
        map.put("companies",companies);
        map.put("total",total);
        return new ResponseResult(200,map);
    }

    @Override
    public ResponseResult<List<Company>> getHotCompanies(Integer currentPage) {
        List<Company> companies = query().list();
        for (Company company : companies) {
            List<PositionDto> positions = positionMapper.getPositionByCompanyId(company.getId())
                    .stream()
                    .limit(3)
                    .collect(Collectors.toList());
            company.setPositions(positions);
        }
        companies = companies.stream()
                                .filter(company -> company.getScale()>=10000)
                                .sorted((o1, o2) -> o2.getPositions().size() - o1.getPositions().size())
                                .skip((currentPage -1) * 3L)
                                .limit(3)
                                .collect(Collectors.toList());
        return new ResponseResult<>(200,companies);
    }

    @Override
    @Transactional
    public ResponseResult<String> updateCompanyInfo(String location, Company company) {
        boolean isSuccess = update().eq("id",company.getId()).update(company);
        boolean isUpdate = companyInfoService.update().eq("company_id", company.getId()).set("location", location).update();
        if(isSuccess && isUpdate) {
            return new ResponseResult<>(200,"企业信息更新成功！");
        }
        return new ResponseResult<>(200,"企业信息更新失败！");
    }

    @Override
    public ResponseResult getBasicInfoByName(String name) {
        List<Company> companies = query().like("name", name).list();
        Map<String,Object> map = new HashMap<>();
        map.put("companies",companies);
        return new ResponseResult<>(200,map);
    }

}
